<?php
require_once '../../server/utils/functions.php';
require_once '../../server/model/Salle.php';
if(isset($_SESSION["USER"]) && checkUserConnected($_SESSION["USER"])){
    
}else{
    echo("Caca0");
    exit(0);
}
$db = db();
$parameters = Array();
if(isset($_POST["SAL_NOM"])&& !empty($_POST["SAL_NOM"])){
    $parameters["SAL_NOM"] = utf8_decode(htmlentities($_POST["SAL_NOM"]));
}else{
    echo("Caca1");
    exit(0);
}    

if(isset($_POST["SAL_INFOS"])&& !empty($_POST["SAL_INFOS"])){
    $parameters["SAL_INFOS"] = utf8_decode(htmlentities($_POST["SAL_INFOS"]));
}   

$preReq = $db->prepare("SELECT MAX(SAL_ID)+1 FROM SALLE");
$preReq->execute();
$preData = $preReq->fetch();
$preReq->closeCursor();
$parameters["SAL_ID"] = $preData[0]==null?0:$preData[0];

$i = 0;
$request =  "";
$request2 = "";
foreach($parameters as $name=>$part){
    $request .= $name;
    $request2 .= ":".$name;
    $i++;
    if($i<sizeof($parameters)){
        $request .= ", ";
        $request2 .= ", ";
    }
}
$req = $db->prepare("INSERT INTO SALLE(".$request.") VALUES(".$request2.")");
if($req->execute($parameters)){
    echo("CacaSuccess");
}else{
    echo json_encode($req->errorInfo());
    echo("Caca4");
}
$req->closeCursor();
?>